﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;

namespace GPSTracker
{
    public partial class RootMaster : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["LoginMaster"] != string.Empty && Session["LoginMaster"] != null)
            {
                var lg = Session["LoginMaster"] as Views.DAL.LoginBO;

                this.SetImg(true);
                this.lblUserName.Text = lg.DisplayName + " ( " + lg.DesignationName + " )";
            }
            else
            {
                this.SetImg(false);
                this.lblUserName.Text = string.Empty;
            }
        }

        public void LogOut()
        {
            try
            {
                Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);

                Response.AddHeader("pragma", "no-cache");
                Response.AddHeader("Cache-Control", "no-cache");
                Response.CacheControl = "no-cache";
                Response.Expires = -1;
                Response.ExpiresAbsolute = DateTime.Now.AddSeconds(-1);
                Response.Cache.SetNoStore();
                HttpContext.Current.Session.Abandon();

                var browser = Request.Browser;
                var name = browser.Browser;
                System.Web.Security.FormsAuthentication.SignOut();
                Session.RemoveAll();
                Session.Clear();
                Session.Abandon();

                Response.Redirect("~/Account/Login.aspx");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void SetImg(bool flag)
        {
            if (flag)
            {
                this.imgCallMe.Visible = false;
                this.lnkLogOut.Visible = true;
                this.lblUserName.Visible = true;                
            }
            else
            {
                this.imgCallMe.Visible = true;
                this.lnkLogOut.Visible = false;
                this.lblUserName.Visible = false;
            }
        }

        protected void btnLogOut_Click(object sender, EventArgs e)
        {
            try
            {
                LogOut();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void HeadLoginStatus_LoggingOut(object sender, LoginCancelEventArgs e)
        {
            try
            {
                LogOut();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
